package com.ruoyi.garbage.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.excel.ExcelUtils;
import com.ruoyi.garbage.domain.QuestionBank;
import com.ruoyi.garbage.service.IQuestionBankService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 题库Controller
 *
 * @author 刘哲贤
 * @date 2023-01-07
 */
@RestController
@RequestMapping("/admin/questionBank")
public class QuestionBankController extends BaseController {
    @Resource
    private IQuestionBankService questionBankService;

    /**
     * 查询题库列表
     */
    @PreAuthorize("@ss.hasPermi('admin:questionBank:list')")
    @GetMapping("/list")
    public TableDataInfo list(QuestionBank questionBank) {
        startPage();
        List<QuestionBank> list = questionBankService.selectQuestionBankList(questionBank);
        return getDataTable(list);
    }

    /**
     * 导出题库列表
     */
    @PreAuthorize("@ss.hasPermi('admin:questionBank:export')")
    @Log(title = "题库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuestionBank questionBank) {
        List<QuestionBank> list = questionBankService.selectQuestionBankList(questionBank);
        ExcelUtils<QuestionBank> util = new ExcelUtils<QuestionBank>(QuestionBank.class);
        util.exportData(response, list, "题库数据");
    }

    /**
     * 获取题库详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:questionBank:query')")
    @GetMapping(value = "/{questionId}")
    public AjaxResult getInfo(@PathVariable("questionId") Long questionId) {
        return AjaxResult.success(questionBankService.selectQuestionBankByQuestionId(questionId));
    }

    /**
     * 新增题库
     */
    @PreAuthorize("@ss.hasPermi('admin:questionBank:add')")
    @Log(title = "题库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QuestionBank questionBank) {
        return toAjax(questionBankService.insertQuestionBank(questionBank));
    }

    /**
     * 修改题库
     */
    @PreAuthorize("@ss.hasPermi('admin:questionBank:edit')")
    @Log(title = "题库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QuestionBank questionBank) {
        return toAjax(questionBankService.updateQuestionBank(questionBank));
    }

    /**
     * 删除题库
     */
    @PreAuthorize("@ss.hasPermi('admin:questionBank:remove')")
    @Log(title = "题库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{questionIds}")
    public AjaxResult remove(@PathVariable Long[] questionIds) {
        return toAjax(questionBankService.deleteQuestionBankByQuestionIds(questionIds));
    }
}
